package com.example.replacebooks.service;

import com.example.replacebooks.common.JwtTokenUtils;
import com.example.replacebooks.entity.Class;
import com.example.replacebooks.entity.Params;
import com.example.replacebooks.exception.CustomException;
import com.example.replacebooks.mapper.ClassMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ClassService {
    @Resource
    private ClassMapper classMapper;

    public List<Class> findAll(){
        return classMapper.selectAll();
    }

    public PageInfo<Class> findBySearch(Params params){
        // 开启分页查询
        PageHelper.startPage(params.getPageNum(),params.getPageSize());
        // 接下来的查询会自动按照当前开启的分页查询
        List<Class> list = classMapper.findBySearch(params);
        return PageInfo.of(list);
    }

    public void add(Class clas) {
        // 1. 分类id一定要有，否则不让新增
        if (clas.getClassid() == null){
            throw new CustomException("分类id不能为空");
        }
        // 2. 进行重复性判断，同一id的分类替换，不同id新增
        Class classid = classMapper.findByClassid(clas.getClassid());
        if (classid != null){
            // 该id已存在
            classMapper.updateByuserid(clas);
        }else{
            classMapper.insertSelective(clas);
        }
    }

    public void delete(Integer userid) {
        classMapper.deleteByPrimaryKey(userid);
    }

}
